package com.lesson01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc2{
    public static void main(String[] args) {
        new Calculator2();
    }
}
//计算器类
class Calculator2 extends Frame {
    //属性
    TextField t1,t2,t3;
    //方法
    public Calculator2() {
        loadFrame();
    }
    public void loadFrame(){
        //组件：
        //3个文本框
        t1 = new TextField(10);
        t2 = new TextField(10);
        t3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new CalculatorListener());
        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);

        pack();
        setVisible(true);
    }
    //监听器类
    class CalculatorListener implements ActionListener {
        //获取变量 组合
        @Override
        public void actionPerformed(ActionEvent e) {
            //获得数字
            int n1 = Integer.parseInt(t1.getText());
            int n2 = Integer.parseInt(t2.getText());
            //运算放回
            t3.setText(""+(n1+n2));
            //清除前面
            t1.setText("");
            t2.setText("");
        }
    }
}
